#include<iostream>
#include"/Users/yjc/Programming/eurekas-algorithm-path/2.Linklist/Linklist.cpp"

ListNode *detectCycle(ListNode *head) 
{
    if(!head)
        return nullptr;
    ListNode *fast=head,*slow=head;
    do
    {
        if(!fast->next)//如果fast的next不为空，则fast的next的next一定不会访问不到
            return nullptr;
        else
            fast=fast->next->next;
        slow=slow->next;
    } while (fast!=slow&&fast);//fast本身可能已空
    if (!fast)//这时必定无环
    {
        return nullptr;
    }
    //两个指针分别从相遇点和起点出发，找入环点
    fast=head;
    while (fast!=slow)
    {
        fast=fast->next;
        slow=slow->next;
    }
    return slow;
}